package com.supeng.PMdepot.activity.bak;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.supeng.PMdepot.ItemDecoration.SpaceItemDecoration;
import com.supeng.PMdepot.R;
import com.supeng.PMdepot.adapter.bak.ListItemAdapter;
import com.supeng.PMdepot.base.BaseActivity;
import com.supeng.PMdepot.common.Config;
import com.supeng.PMdepot.common.Constant;
import com.supeng.PMdepot.entity.bak.BaoXiuDetailInfo;
import com.supeng.PMdepot.entity.ServiceResult;
import com.supeng.PMdepot.entity.bak.WxJiluInfo;
import com.supeng.PMdepot.event.FinishEvent;
import com.supeng.PMdepot.event.ShuaXinEvent;
import com.supeng.PMdepot.event.ShuaXinJiluEvent;
import com.supeng.PMdepot.net.Api;
import com.supeng.PMdepot.net.NetUtils;
import com.supeng.PMdepot.util.ImageLoaderUtil;
import com.supeng.PMdepot.util.ToastUtils;
import com.supeng.PMdepot.util.Utils;
import com.supeng.PMdepot.util.actionbar.LightStatusBarUtils;
import com.supeng.PMdepot.util.actionbar.ToolbarUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Supeng on 2018/10/12.
 */

public class BaoXiuDetailActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    @BindView(R.id.title_bar)
    LinearLayout titleBar;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.vv_line)
    View vvLine;
    @BindView(R.id.btn_1)
    Button btn1;
    @BindView(R.id.btn_2)
    Button btn2;
    @BindView(R.id.btn_3)
    Button btn3;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    private BaseQuickAdapter homeAdapter;
    private String doingType;
    private String wxid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        doingType=getIntent().getStringExtra(Constant.DOING_TYPE);
        wxid=getIntent().getStringExtra("wxid");
        setContentView(R.layout.activity_baoxiu_all_detail);
        unbinder = ButterKnife.bind(this);
        //标题栏颜色
        ToolbarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.blueColorAppKey));
        //深色主题 true字颜色为深色 false 为白色标题字
        LightStatusBarUtils.setLightStatusBar(this, false);
        initView();
    }

    private void initView() {
        //报修人
        if(Config.getUserInfo().getRole().equals(Constant.BAOXIUREN)){
            if(doingType.equals(Constant.WEICHULI)){
                btn1.setText(getResources().getString(R.string.buchong));
                btn3.setText(getResources().getString(R.string.chexiao));
                btn2.setVisibility(View.GONE);
                tvDelete.setVisibility(View.GONE);
            }else if(doingType.equals(Constant.ChuLIZHONG)){
                btn1.setVisibility(View.GONE);
                btn3.setVisibility(View.GONE);
                btn2.setVisibility(View.GONE);
                tvDelete.setVisibility(View.GONE);
            }else if(doingType.equals(Constant.YICHULI)){
                btn1.setVisibility(View.GONE);
                btn3.setVisibility(View.GONE);
                btn2.setVisibility(View.GONE);
                tvDelete.setVisibility(View.GONE);
                llBottom.setVisibility(View.GONE);
                vvLine.setVisibility(View.GONE);
            }
        }
        //维修人
        else if(Config.getUserInfo().getRole().equals(Constant.WEIXIUREN)){
            if(doingType.equals(Constant.WEICHULI)){
                btn1.setText(getResources().getString(R.string.shouli));
                btn3.setVisibility(View.GONE);
                btn2.setVisibility(View.GONE);
                tvDelete.setVisibility(View.GONE);
            }else if(doingType.equals(Constant.ChuLIZHONG)){
                btn1.setText(getResources().getString(R.string.buchong));
                btn3.setText(getResources().getString(R.string.tuihui));
                btn2.setText(getResources().getString(R.string.jiedan));
                tvDelete.setVisibility(View.GONE);
            }else if(doingType.equals(Constant.YICHULI)){
                btn1.setVisibility(View.GONE);
                btn3.setVisibility(View.GONE);
                btn2.setVisibility(View.GONE);
                tvDelete.setVisibility(View.GONE);
                llBottom.setVisibility(View.GONE);
                vvLine.setVisibility(View.GONE);
            }
        }
        //管理员
        else if(Config.getUserInfo().getRole().equals(Constant.GUANLIYUAN)){
            if(doingType.equals(Constant.WEICHULI)){
                btn1.setText(getResources().getString(R.string.shouli));
                btn3.setVisibility(View.GONE);
                btn2.setVisibility(View.GONE);
            }else if(doingType.equals(Constant.ChuLIZHONG)){
                btn1.setText(getResources().getString(R.string.buchong));
                btn3.setText(getResources().getString(R.string.tuihui));
                btn2.setText(getResources().getString(R.string.jiedan));
            }else if(doingType.equals(Constant.YICHULI)){
                btn1.setVisibility(View.GONE);
                btn3.setText(getResources().getString(R.string.tuihui));
                btn2.setVisibility(View.GONE);
               // tvDelete.setVisibility(View.GONE);
            }
        }
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new ShuaXinEvent(doingType));
                finish();
            }
        });
        initAdapter();
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        titleBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(homeAdapter!=null&&homeAdapter.getData()!=null&&homeAdapter.getData().size()>0){
                    rvList.scrollToPosition(0);
                }
            }
        });
        tvDelete.setOnClickListener(this);
        loadDetailByServer(wxid);
    }
    private TextView tv_danhao;
    private TextView tv_sb_name;
    private TextView tv_bianhao;
    private TextView tv_dmiaosu;
    private ImageView iv_photo1;
    private ImageView iv_photo2;
    private TextView tv_photomore;
    private TextView tv_baoxiuren;
    private TextView tv_baoxiutimer;
    private TextView tv_shouliren;
    private TextView tv_shoulitimer;
    private TextView tv_weixiumiaoshu;
    private TextView tv_weixiuphoto;
    private TextView tv_weixiuren;
    private TextView tv_weixiutimer;
    private LinearLayout ll_wxphoto;
    private LinearLayout ll_none;
    private View vv_none;
    LinearLayout llphoto;
    private String[] arrimgs;

    @Override
    public void onBackPressed() {
        EventBus.getDefault().post(new ShuaXinEvent(doingType));
        super.onBackPressed();

    }

    public void setUI(BaoXiuDetailInfo.DataBean dataBean){
        tv_danhao.setText("报修单号:"+dataBean.getWxid());
        tv_sb_name.setText(dataBean.getMacName()+"("+dataBean.getModelNo()+")");
        tv_bianhao.setText(dataBean.getMacID());

        String img=dataBean.getImg();
        if(!TextUtils.isEmpty(img)){
            arrimgs=img.split("\\^");
            if(arrimgs.length==1){
                iv_photo2.setVisibility(View.GONE);
                ImageLoaderUtil.loadImage(getActivity(),Constant.getBaseUrl()+arrimgs[0],iv_photo1);
                tv_photomore.setVisibility(View.GONE);
            }
            if(arrimgs.length>=2){
                ImageLoaderUtil.loadImage(getActivity(),Constant.getBaseUrl()+arrimgs[0],iv_photo1);
                ImageLoaderUtil.loadImage(getActivity(),Constant.getBaseUrl()+arrimgs[1],iv_photo2);
                if(arrimgs.length>2){
                    tv_photomore.setText("照片("+arrimgs.length+")");
                }else {
                    tv_photomore.setVisibility(View.GONE);
                }
            }
        }else{
            llphoto.setVisibility(View.GONE);
        }
        if(doingType.equals(Constant.YICHULI)||doingType.equals(Constant.ChuLIZHONG)){
            llphoto.setVisibility(View.GONE);
            setText(tv_dmiaosu,dataBean.getMtncContent(),img);
        }else{
            tv_dmiaosu.setText(dataBean.getMtncContent());
        }

        tv_baoxiuren.setText(dataBean.getAppName());
        tv_baoxiuren.setTag(dataBean.getAppPhone());
        tv_baoxiutimer.setText(Utils.timerParse(dataBean.getAppTime()));

        tv_shouliren.setText(dataBean.getRecName());
        tv_shouliren.setTag(dataBean.getRecPhone());
        tv_shoulitimer.setText(Utils.timerParse(dataBean.getRecTime()));
    }
    public void setText(TextView tvCount,String context,String img) {
        String counStr="";
        if(!TextUtils.isEmpty(img)){
            final String[] arrimgs=img.split("\\^");
            counStr=context + "照片("+arrimgs.length+")";
            SpannableStringBuilder style = new SpannableStringBuilder(counStr);
            style.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.blueColorAppKey)), context.length(), counStr.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //颜色
            tvCount.setText(style);
            tvCount.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent5=new Intent(getActivity(),PicturePreviewListActivity.class);
                    intent5.putExtra("urls", arrimgs);
                    intent5.putExtra("position",0);
                    startActivity(intent5);
                }
            });
        }else{
            tvCount.setText(context);
        }

    }
    private void loadDetailByServer(String wxid) {
        Api.baoXiuDetail(getActivity(), wxid, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if(rspData.getRecode()==1){
                    BaoXiuDetailInfo baoXiuDetailInfo= (BaoXiuDetailInfo) rspData;
                    BaoXiuDetailInfo.DataBean dataBean=baoXiuDetailInfo.getData();
                    setUI(dataBean);
                }else{
                    ToastUtils.show(rspData.getMsg());
                }
            }

            @Override
            public void failed(String msg) {
                ToastUtils.show(msg);
            }

            @Override
            public void onUsedCache(String msg) {

            }
        }, BaoXiuDetailInfo.class);
    }

    private void initAdapter() {

        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvList.addItemDecoration(new SpaceItemDecoration(getActivity()).setSpace(0.5f).setSpaceColor(getResources().getColor(R.color.item_press2)));
        homeAdapter = new ListItemAdapter(null);
        homeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_name:
                        WxJiluInfo.ListBean listBean = (WxJiluInfo.ListBean) adapter.getData().get(position);
                        Intent intent7=new Intent(getActivity(),CallPhoneActivity.class);
                        intent7.putExtra("name",listBean.getAppName());
                        intent7.putExtra("phone",listBean.getAppPhone());
                        startActivity(intent7);
                        break;
                    case R.id.tv_miaosu:
                        Intent intent8=new Intent(getActivity(),PicturePreviewListActivity.class);
                        WxJiluInfo.ListBean listBean2 = (WxJiluInfo.ListBean) adapter.getData().get(position);
                        String[] arrimgs=listBean2.getImg().split("\\^");
                        intent8.putExtra("urls", arrimgs);
                        intent8.putExtra("position",0);
                        startActivity(intent8);
                        break;
                }
            }
        });
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_baoxiu_detail, null);
        tv_danhao=view.findViewById(R.id.tv_danhao);
        tv_sb_name=view.findViewById(R.id.tv_sb_name);
        tv_bianhao=view.findViewById(R.id.tv_bianhao);
        tv_dmiaosu=view.findViewById(R.id.tv_dmiaosu);
        iv_photo1=view.findViewById(R.id.iv_photo1);
        iv_photo2=view.findViewById(R.id.iv_photo2);
        tv_photomore=view.findViewById(R.id.tv_photomore);
        tv_baoxiuren=view.findViewById(R.id.tv_dbaoxiuren);
        tv_baoxiutimer=view.findViewById(R.id.tv_baoxiutimer);
        tv_shouliren=view.findViewById(R.id.tv_dshouliren);
        tv_shoulitimer=view.findViewById(R.id.tv_shoulitimer);
        tv_weixiumiaoshu=view.findViewById(R.id.tv_weixiumiaoshu);
        tv_weixiuphoto=view.findViewById(R.id.tv_weixiuphoto);
        tv_weixiuren=view.findViewById(R.id.tv_weixiuren);
        tv_weixiutimer=view.findViewById(R.id.tv_weixiutimer);
        ll_wxphoto=view.findViewById(R.id.ll_wxphoto);
        ll_none=view.findViewById(R.id.ll_none);
        vv_none=view.findViewById(R.id.vv_none);
        iv_photo1.setOnClickListener(this);
        iv_photo2.setOnClickListener(this);
        tv_photomore.setOnClickListener(this);
        homeAdapter.addHeaderView(view);
        rvList.setAdapter(homeAdapter);
        tv_shoulitimer.setOnClickListener(this);
        tv_baoxiuren.setOnClickListener(this);
        tv_shouliren.setOnClickListener(this);
        quanxian(view);

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShuaXinJiluEvent(ShuaXinJiluEvent event) {
       loadByServer();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFinishEvent(FinishEvent event) {
        finish();
    }

    public void quanxian(View view){
        llphoto=view.findViewById(R.id.ll_photo);
        LinearLayout shouliren=view.findViewById(R.id.ll_shouliren);
        LinearLayout weixiuxinxi=view.findViewById(R.id.ll_weixiuxinxi);
        LinearLayout buchongxinxi=view.findViewById(R.id.ll_buchongjilu);
        if(doingType.equals(Constant.WEICHULI)){
            shouliren.setVisibility(View.GONE);
            weixiuxinxi.setVisibility(View.GONE);
           // buchongxinxi.setVisibility(View.GONE);
            loadByServer();
        }else if(doingType.equals(Constant.ChuLIZHONG)){
            llphoto.setVisibility(View.GONE);
            weixiuxinxi.setVisibility(View.GONE);
            loadByServer();
        }else if(doingType.equals(Constant.YICHULI)){
            llphoto.setVisibility(View.GONE);
            loadByServer();
        }
        //报修人
        if(Config.getUserInfo().getRole().equals(Constant.BAOXIUREN)){
            if(doingType.equals(Constant.WEICHULI)){

            }else if(doingType.equals(Constant.ChuLIZHONG)){

            }else if(doingType.equals(Constant.YICHULI)){

            }
        }
        //维修人
        else if(Config.getUserInfo().getRole().equals(Constant.WEIXIUREN)){
            if(doingType.equals(Constant.WEICHULI)){

            }else if(doingType.equals(Constant.ChuLIZHONG)){

            }else if(doingType.equals(Constant.YICHULI)){

            }
        }
        //管理员
        else if(Config.getUserInfo().getRole().equals(Constant.GUANLIYUAN)){
            if(doingType.equals(Constant.WEICHULI)){

            }else if(doingType.equals(Constant.ChuLIZHONG)){

            }else if(doingType.equals(Constant.YICHULI)){

            }
        }
    }
    public void doShouLi(String status){
        Api.updateStatus(getActivity(), wxid, status,"",new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if(rspData.getRecode()==1){
                    EventBus.getDefault().post(new ShuaXinEvent(Constant.WEICHULI));
                    EventBus.getDefault().post(new ShuaXinEvent(Constant.ChuLIZHONG));
//                    if(doingType.equals(Constant.WEICHULI))
//                    EventBus.getDefault().post(new CurrentNowEvent(1));
//                    if(doingType.equals(Constant.ChuLIZHONG))
//                        EventBus.getDefault().post(new CurrentNowEvent(2));
                    //受理
                    finish();
                }else{
                    ToastUtils.show(rspData.getMsg());
                }
            }

            @Override
            public void failed(String msg) {
                ToastUtils.show(msg);
            }

            @Override
            public void onUsedCache(String msg) {

            }
        }, ServiceResult.class);
    }
    public void loadByServer(){
        Api.wXMingXiList(getActivity(), wxid,new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if(rspData.getRecode()==1){
                    WxJiluInfo wxJiluInfo= (WxJiluInfo) rspData;
                    WxJiluInfo.ListBean finishBean=wxJiluInfo.getFinish();
                    List<WxJiluInfo.ListBean> jiLuListBean=wxJiluInfo.getList();
                    homeAdapter.setNewData(jiLuListBean);
                    if(jiLuListBean.size()>0){
                        ll_none.setVisibility(View.GONE);
                        vv_none.setVisibility(View.GONE);
                    }else{
                        ll_none.setVisibility(View.VISIBLE);
                        vv_none.setVisibility(View.VISIBLE);
                    }
                    if(finishBean!=null){
                        setFinishUI(finishBean);
                    }
                }else{
                    ToastUtils.show(rspData.getMsg());
                }
            }

            @Override
            public void failed(String msg) {
                ToastUtils.show(msg);
            }

            @Override
            public void onUsedCache(String msg) {

            }
        }, WxJiluInfo.class);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //补充
            case R.id.btn_1:
                if(btn1.getText().equals(getResources().getString(R.string.shouli))){
                    doShouLi("sl");
                }else {
                    Intent intent=new Intent(getActivity(), BuChongActivity.class);
                    intent.putExtra("wxid",wxid);
                    startActivity(intent);
                }
                break;
            case R.id.btn_2:
                Intent intent= new Intent(getActivity(),BuChongActivity.class);
                intent.putExtra("type","jiedan");
                intent.putExtra("wxid",wxid);
                startActivity(intent);
                break;
            case R.id.btn_3:
                    Intent intent2 = new Intent(getActivity(), CheXiaoActivity.class);
                    intent2.putExtra("doingType",doingType);
                intent2.putExtra("wxid",wxid);
                if(btn3.getText().equals(getResources().getString(R.string.tuihui))){
                    intent2.putExtra("type", "tuihui");
                }else {
                    intent2.putExtra("type", "chexiao");
                }
                    startActivity(intent2);
                break;
            case R.id.tv_delete:
                Intent intent3= new Intent(getActivity(),CheXiaoActivity.class);
                intent3.putExtra("doingType",doingType);
                intent3.putExtra("wxid",wxid);
                intent3.putExtra("type","delete");
                startActivity(intent3);
                break;
            case R.id.iv_photo1:
                Intent intent4=new Intent(getActivity(),PicturePreviewListActivity.class);
                intent4.putExtra("urls", arrimgs);
                intent4.putExtra("position",0);
                startActivity(intent4);
                break;
            case R.id.iv_photo2:
                Intent intent5=new Intent(getActivity(),PicturePreviewListActivity.class);
                intent5.putExtra("urls", arrimgs);
                intent5.putExtra("position",1);
                startActivity(intent5);
                break;
            case R.id.tv_photomore:
                Intent intent6=new Intent(getActivity(),PicturePreviewListActivity.class);
                intent6.putExtra("urls", arrimgs);
                if(arrimgs.length>2)
                intent6.putExtra("position",2);
                startActivity(intent6);
                break;
            case R.id.tv_dbaoxiuren:
                Intent intent7=new Intent(getActivity(),CallPhoneActivity.class);
                intent7.putExtra("name",tv_baoxiuren.getText().toString());
                intent7.putExtra("phone",tv_baoxiuren.getTag().toString());
                startActivity(intent7);
                break;
            case R.id.tv_dshouliren:
                Intent intent8=new Intent(getActivity(),CallPhoneActivity.class);
                intent8.putExtra("name",tv_shouliren.getText().toString());
                intent8.putExtra("phone",tv_shouliren.getTag().toString());
                startActivity(intent8);
                break;
        }
    }

    public void setFinishUI(final WxJiluInfo.ListBean finishUI) {
       tv_weixiumiaoshu.setText(finishUI.getWxContent());
       String img=finishUI.getImg();
        if(!TextUtils.isEmpty(img)){
            final String[] arrimgs=img.split("\\^");
            ll_wxphoto.setVisibility(View.VISIBLE);
            tv_weixiuphoto.setText("照片("+arrimgs.length+")");
            tv_weixiuphoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent4=new Intent(getActivity(),PicturePreviewListActivity.class);
                    intent4.putExtra("urls", arrimgs);
                    intent4.putExtra("position",0);
                    startActivity(intent4);
                }
            });

        }else {
            ll_wxphoto.setVisibility(View.GONE);
        }
        tv_weixiuren.setText(finishUI.getAppName());
        tv_weixiutimer.setText(Utils.timerParse(finishUI.getAppTime()));
        tv_weixiuren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent7=new Intent(getActivity(),CallPhoneActivity.class);
                intent7.putExtra("name",finishUI.getAppName());
                intent7.putExtra("phone",finishUI.getAppPhone());
                startActivity(intent7);
            }
        });
    }
}
